package basic;

import java.util.HashSet;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class sg4 {

	/**
	 * @param args
	 */
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception{
		if(args.length!=2){
			System.err.println("User path err!!");
			System.exit(-1);
		}
		Job job = new Job(new Configuration(),"sougou");
		job.setJarByClass(sg4.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job,new Path(args[1]));
		
		job.setMapperClass(bmap.class);
		job.setReducerClass(breduce.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		job.waitForCompletion(true);
		
	}
	public static class bmap extends Mapper<LongWritable, Text,Text,NullWritable>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("\t");
			String i=lines[0].toString().trim();
			String sub = i.substring(8,10);
			int parseInt = Integer.parseInt(sub);
			if(lines.length==6 && lines[2].toString().trim().contains("百度影音") && parseInt>=7  && parseInt<10){
				String uid=lines[1].toString().trim();
				context.write(new Text(uid),NullWritable.get());
			}
		};
	}
	public  static class breduce extends Reducer<Text,NullWritable, Text, NullWritable>{
		Set<String> sets = new HashSet<String>();
		protected void reduce(Text k2, java.lang.Iterable<NullWritable> values, org.apache.hadoop.mapreduce.Reducer<Text,NullWritable,Text,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			sets.add(k2.toString());
		};
		protected void cleanup(org.apache.hadoop.mapreduce.Reducer<Text,NullWritable,Text,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			context.write(new Text("the number of search 'baiduyingyin' users :"
					+ sets.size()), NullWritable.get());
		};
	}

}
